// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/maintain/api/AppPlanTaskApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/maintain/model/AppQueryPlanTaskAssetListReq'
import OneClass from '@api/maintain/model/AppQueryPlanTaskAssetDetailReq'
import EditClass from '@api/maintain/model/AppHandleTaskReq'
import BaseClass from '@api/maintain/model/AppPlanTaskAssetDto'
import FileClass from '@api/maintain/model/AppPlanTaskAssetFileDto'
import ChildClass from '@api/maintain/model/AppPlanTaskAssetItemResultDto'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const BASE = [BaseClass, 'planTaskAssetDto']

const CHILDREN = [ChildClass, 'taskAssetItemResultDto', 1, (dto, obj) => {
  const imageList = obj.image.map((fileUrl) => ({ fileUrl, fileName: fileUrl.split('/').pop(), fileType: 1 }))
  CORE.grpcDTO(dto, obj, [FileClass, 'taskAssetItemFileDto', imageList])
}]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  validParam(param, state, root) {
    const { id, handleStatus } = root.state.one
    return id && handleStatus === param.handleStatus
  },
  transParam(param, { rootState: { one: { id } } }) {
    return { ...param, planTaskId: id }
  },
  async getList(param, TIP = `查询任务${CORE.MAP.ASSET}列表`) {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryAppPlanTaskAssetList', DTO)
    return [resp.comQuery.totalCount, resp.planTaskAssetDto || []]
  },
  transList(arr, { rootState: { one: { id } } }) {
    arr.forEach((item) => {
      item.planTaskId = id
    })
    return arr
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  validOne(one) {
    return one.planTaskId
  },
  async getOne(one, TIP = `查询任务${CORE.MAP.ASSET}详情`) {
    const DTO = CORE.grpcDTO(OneClass, one)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryAppPlanTaskAssetDetail', DTO)
    const { planTaskAssetDto = {}, taskAssetFileDto = [], taskAssetItemResultDto = [] } = resp
    return { ...planTaskAssetDto, taskAssetFileDto, taskAssetItemResultDto }
  },
  transOne(obj) {
    obj.taskAssetItemResultDto.forEach((item, i) => {
      item.index = i + 1
      item.id = item.id || item.planTaskTemplateItemId || item.index
      item.image = item.taskAssetItemFileDto.map((file) => file.fileUrl)

      item.type = item.itemResultType // 简化长度

      obj[`result_${item.id}_${item.itemResultType}`] = ''
      if (item.imageRequire === 1) obj[`result_${item.id}_4`] = ''
    })

    obj.image = obj.taskAssetFileDto.map((file) => file.fileUrl)

    return obj
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '处理') {
    const imageList = one.image.map((fileUrl) => ({ fileUrl, fileName: fileUrl.split('/').pop(), fileType: 1 }))
    const DTO = CORE.grpcDTO(EditClass, one, BASE, [FileClass, 'taskAssetFileDto', imageList], CHILDREN)
    await CORE.grpc(TIP, CLIENT, 'handleAppTask', DTO)
    return CORE.grpcOK(TIP)
  },
}
